package com.clearmine.minesweeper.controller;

import com.clearmine.minesweeper.model.GameMessage;
import com.clearmine.minesweeper.service.GameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;

@Controller
public class GameController {

    @Autowired
    private GameService gameService;

    @MessageMapping("/createRoom")
    @SendTo("/topic/game")
    public GameMessage createRoom(GameMessage message) {
        GameMessage room = gameService.createRoom(message.getPlayerId());
        room.setUserId(message.getUserId());
        return room;
    }

    @MessageMapping("/joinRoom")
    @SendTo("/topic/game")
    public GameMessage joinRoom(GameMessage message) {
        GameMessage gameMessage = gameService.joinRoom(message.getRoomId(), message.getPlayerId());
        gameMessage.setUserId(message.getUserId());
        return gameMessage;
    }

    @MessageMapping("/gameAction")
    @SendTo("/topic/game")
    public GameMessage handleGameAction(GameMessage message) {
        GameMessage gameMessage = gameService.handleGameAction(message);
        return gameMessage;
    }
}